package cc.toprank.syncdatatask.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import cc.toprank.syncdatatask.pojo.MsgInfo;

public class FieldConvertHelper {  
	
	
	private static FieldConvertHelper instance = new FieldConvertHelper();

	private FieldConvertHelper() {

	}

	public synchronized static FieldConvertHelper getInstance() {
		if (instance == null) {
			instance = new FieldConvertHelper();
		}
		
		return instance;
	}
	   
	public  byte[] getByte(MsgInfo msgInfo) {
		byte[] bt = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		try {
			if (msgInfo != null) {
				ObjectOutputStream objos = new ObjectOutputStream(baos);
				objos.writeObject(msgInfo);
				bt = baos.toByteArray();
			}
		} catch (Exception e) {
			bt = (byte[]) null;
			e.printStackTrace();
		}
		return bt;
	} 
	
	
	public  MsgInfo  getMsgInfoFromByteArray(byte[] byteData) throws IOException, ClassNotFoundException { 
		Object obj = null; 
		MsgInfo msgInfo = null; 
		// bytearray to object
		ByteArrayInputStream bi = new ByteArrayInputStream(byteData);
		ObjectInputStream oi = new ObjectInputStream(bi);
		obj = oi.readObject();
		bi.close();
		oi.close();
		
		if (obj != null ) return msgInfo = (MsgInfo) obj;
		return msgInfo;
	} 
	

}
